package alex.rsx.view;

import java.util.Vector;

import alex.rsx.activity.ReadBookActivity;
import alex.rsx.activity.r.R;
import alex.rsx.anim.Rotate3dAnimation;
import alex.rsx.data.MenuItem;
import alex.rsx.data.StaticData;
import alex.rsx.util.TestData;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;
import android.widget.ViewSwitcher;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.LinearLayout.LayoutParams;

public class AsideContainer implements OnClickListener, OnChildClickListener, OnItemClickListener {

	private Context mContent;
	private LayoutInflater mLayoutInflater;
	private ExpertDrawer mExpertDrawer;

	public AsideContainer(Context context, ExpertDrawer expertDrawer) {
		mContent = context;
		mLayoutInflater = LayoutInflater.from(context);
		mExpertDrawer = expertDrawer;
		jointIndex();
	}

	/**
	 * 主界面左边分类列表
	 * 
	 * @return
	 */
	public RelativeLayout getMainLeftView() {
		RelativeLayout leftMain = (RelativeLayout) mLayoutInflater.inflate(R.layout.main_left, null);
		ExpandableListView mainLeftMenu = (ExpandableListView) leftMain.findViewById(R.id.main_left_menu);
		MainLeftAdapter adapter = new MainLeftAdapter(mContent);
		adapter.setOn(this);
		mainLeftMenu.setGroupIndicator(null);
		mainLeftMenu.setAdapter(adapter);
		mainLeftMenu.setOnChildClickListener(this);
		return leftMain;
	}

	
	/**
	 * 主界面右边用户列表
	 * 
	 * @return
	 */
	public LinearLayout getMainRightView() {
		LinearLayout rightMain = (LinearLayout) mLayoutInflater.inflate(
				R.layout.main_right, null);
		Button login = (Button) rightMain.findViewById(R.id.login_bt);

		LinearLayout personalList = (LinearLayout) rightMain
				.findViewById(R.id.personal_list);
		Vector<MenuItem> items = StaticData.getPersonalItems();
		for (int i = 0; i < items.size(); i++) {
			LinearLayout l = new LinearLayout(mContent);
			l.setOrientation(LinearLayout.VERTICAL);
			Button but = new Button(mContent);
			l.setGravity(Gravity.CENTER);
			but.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT));
			but.setTextColor(0xffbbbbbb);
			but.setBackgroundDrawable(null);
			but.setText(items.get(i).name);
			but.setTextSize(16);
			but.setId(items.get(i).tag);
			but.setOnClickListener(this);
			but.setCompoundDrawablesWithIntrinsicBounds(items.get(i).icon, 0,0, 0);
			but.setCompoundDrawablePadding(10);
			l.addView(but);
			ImageView line = new ImageView(mContent);
			line.setBackgroundResource(R.drawable.main_left_line);
			l.addView(line);
			
			personalList.addView(l);
		}

		LinearLayout personalLogin = (LinearLayout) rightMain.findViewById(R.id.personal_login);
		personalLogin.setVisibility(View.GONE);
		final ViewGroup mContainer = (ViewGroup) rightMain.findViewById(R.id.container);
		mContainer.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				final float centerX = mContainer.getWidth() / 2.0f;
				final float centerY = mContainer.getHeight() / 2.0f;

				// Create a new 3D rotation with the supplied parameter
				// The animation listener is used to trigger the next animation
				final Rotate3dAnimation rotation = new Rotate3dAnimation(-1,
						180, centerX, centerY, 0.0f, true);
				rotation.setDuration(500);
				rotation.setFillAfter(true);
				rotation.setInterpolator(new AccelerateInterpolator());

				mContainer.startAnimation(rotation);
			}
		});
		login.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mExpertDrawer.closeRight();
				TextView t = new TextView(mContent);
				mExpertDrawer.setContentView(t);
			}
		});
		return rightMain;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();
		switch (v_id) {
		case StaticData.ID_PERSONAL_BOOKSHELF:
			jointPersonalBookShelf();
			break;
		case StaticData.ID_PERSONAL_ACHIEVEMENT:
			jointPersonalachievement();
			break;
		case StaticData.ID_PERSONAL_INFORMATION:
			jointPersonalInfomation();
			break;
		case StaticData.ID_CLASSIFY_1:
			jointClassify();
			break;
		case StaticData.ID_CLASSIFY_2:
			jointClassify();
			break;
		case StaticData.ID_CLASSIFY_3:
			jointClassify();
			break;
		case StaticData.ID_CLASSIFY_4:
			jointClassify();
			break;
		default:
			break;
		}
	}
	

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {
		// TODO Auto-generated method stub
		if(groupPosition == 1){
			jointRank();
		}else if(groupPosition == 3){
			jointSubject();
		}
		return false;
	}
	
	
	
	/**
	 * 个人书架
	 */
	private void jointPersonalBookShelf(){
		mExpertDrawer.closeLeft();
		RelativeLayout r = (RelativeLayout)mLayoutInflater.inflate(R.layout.main_view_personal_bookshelf, null);
		mExpertDrawer.setContentView(r);
	}
	
	/**
	 * 个人信息
	 */
	private void jointPersonalInfomation(){
		mExpertDrawer.closeLeft();
		RelativeLayout r = (RelativeLayout)mLayoutInflater.inflate(R.layout.main_view_personal_information, null);
		mExpertDrawer.setContentView(r);
	}
	
	/**
	 * 个人成就
	 */
	private void jointPersonalachievement(){
		mExpertDrawer.closeLeft();
		LinearLayout r = (LinearLayout)mLayoutInflater.inflate(R.layout.main_view_personal_achievement, null);
		CircleProgress c11 = (CircleProgress)r.findViewById(R.id.circle11);
		c11.setMainProgress(30);
		c11.setUpValue("24");
		c11.setDownValue("总计看过的书本书");
		
		CircleProgress c12 = (CircleProgress)r.findViewById(R.id.circle12);
		c12.setMainProgress(30);
		c12.setUpValue("448");
		c12.setDownValue("总计翻页数");
		
		CircleProgress c21 = (CircleProgress)r.findViewById(R.id.circle21);
		c21.setMainProgress(30);
		c21.setUpValue("0.3");
		c21.setDownValue("阅读时间");
		
		CircleProgress c22 = (CircleProgress)r.findViewById(R.id.circle22);
		c22.setMainProgress(30);
		c22.setUpValue("6.5");
		c22.setDownValue("每天阅读时间");
		
		mExpertDrawer.setContentView(r);
	}
	
	/**
	 * 排行下书籍列表
	 */
	private void jointRank(){
		mExpertDrawer.closeLeft();
		RelativeLayout r = (RelativeLayout)mLayoutInflater.inflate(R.layout.main_view_rank, null);
		PullToRefreshListView listView = (PullToRefreshListView) r.findViewById(R.id.listView);		
		RankAdapter adapter = new RankAdapter(mContent);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		mExpertDrawer.setContentView(r);
	}
	
	/**
	 * 专题下书籍列表
	 */
	private void jointSubject(){
		mExpertDrawer.closeLeft();
		LinearLayout r = (LinearLayout)mLayoutInflater.inflate(R.layout.main_view_subject, null);
		PullToRefreshListView listView = (PullToRefreshListView) r.findViewById(R.id.listView);		
		listView.setAdapter(TestData.testAdapter(mContent));
		mExpertDrawer.setContentView(r);
	}
	
	/**
	 * 分类下书籍列表
	 */
	private void jointClassify(){
		mExpertDrawer.closeLeft();
		RelativeLayout r = (RelativeLayout)mLayoutInflater.inflate(R.layout.main_view_classify, null);
		PullToRefreshListView listView = (PullToRefreshListView) r.findViewById(R.id.listView);		
		IndexRecommodAdapter adapter = new IndexRecommodAdapter(mContent);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		mExpertDrawer.setContentView(r);
	}
	
	int mIndex = 0;
	private void jointIndex(){
		RelativeLayout r = (RelativeLayout)mLayoutInflater.inflate(R.layout.main_view_index, null);
		ImageButton but1 = (ImageButton)r.findViewById(R.id.text1);
		but1.setOnClickListener(li);
		but1.setId(0);
		ImageButton but2 = (ImageButton)r.findViewById(R.id.text2);
		but2.setOnClickListener(li);
		but2.setId(1);
		ImageButton but3 = (ImageButton)r.findViewById(R.id.text3);
		but3.setOnClickListener(li);
		but3.setId(2);
		PullToRefreshListView listView = (PullToRefreshListView) r.findViewById(R.id.index_list);
		IndexRecommodAdapter adapter = new IndexRecommodAdapter(mContent);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		mExpertDrawer.setContentView(r);
	}
	

	private MyOnli li = new MyOnli();
	class MyOnli implements OnClickListener{
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			int index = v.getId();
			int off = mIndex - index;

		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		Log.e("", "position:"+position);
		Intent intent = new Intent();
		intent.setClass(mContent, ReadBookActivity.class);
		mContent.startActivity(intent);
	}
}
